<?php
// +----------------------------------------------------------------------
// | quanli team
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2020 http://www.fistforward.com/ All rights reserved.
// +----------------------------------------------------------------------
// | Author: allen.qiang
// +----------------------------------------------------------------------

/**
 * 常见问题类
 * @package  module
 * @author   allen.qiang
 * @version 1.0
 */

class sysFaq {

    private $dbca;

    public function __construct( $dbca ) {
        $this->dbca = $dbca;
    }

    /**
     * 常见问题分类列表
     *
     * @param unknown_type $arg_array
     */
    function searchFaqCategoryList(&$arg_array){
        //sql字符串
        $sql = '';

        //参数类型字符串
        $params_type = '';

        //参数值数组
        $params_array = array();

        if (isset($arg_array['branch_id']) && is_numeric($arg_array['branch_id'])) {
            $sql .= ' AND branch_id = ?';
            $params_type .= 'i';
            $params_array[] = $arg_array['branch_id'];
        }

        if (isset($arg_array['vehicle_id']) && is_numeric($arg_array['vehicle_id'])) {
            $sql .= ' AND vehicle_id = ?';
            $params_type .= 'i';
            $params_array[] = $arg_array['vehicle_id'];
        }

        if (isset($arg_array['branch_name']) && !empty($arg_array['branch_name'])) {
            $sql .= ' AND branch_name LIKE ?';
            $params_type .= 's';
            $params_array[] = '%' . trim($arg_array['branch_name']) . '%';
        }

        if (isset($arg_array['branch_code']) && !empty($arg_array['branch_code'])) {
            $sql .= ' AND branch_code = ?';
            $params_type .= 's';
            $params_array[] = trim($arg_array['branch_code']);
        }

        if (isset($arg_array['provinceid']) && is_numeric($arg_array['provinceid'])) {
            $sql .= ' AND provinceid = ?';
            $params_type .= '';
            $params_array[] = $arg_array['provinceid'];
        }

        if (isset($arg_array['cityid']) && is_numeric($arg_array['cityid'])) {
            $sql .= ' AND cityid = ?';
            $params_type .= 'i';
            $params_array[] = $arg_array['cityid'];
        }

        if (isset($arg_array['countyid']) && is_numeric($arg_array['countyid'])) {
            $sql .= ' AND countyid = ?';
            $params_type .= 'i';
            $params_array[] = $arg_array['countyid'];
        }

        if (isset($arg_array['useFlg']) && in_array($arg_array['useFlg'], array(0, 1))) {
            $sql .= ' AND useFlg = ?';
            $params_type .= 'i';
            $params_array[] = $arg_array['useFlg'];
        }

        if ($sql) {
            $sql = 'WHERE ' . substr($sql, 4);
        }

        try {

            if (isset($arg_array['rp']) && $arg_array['rp'] > 1) {

                //统计数量
                $search_sql = 'SELECT COUNT(*) AS item_count FROM sys_branch ' . $sql;
                $count_array = &$this->dbca->stmt_query($search_sql, $params_type, $params_array);
                $item_count = $count_array[0]->item_count;
                if ($item_count == 0) {
                    $arg_array['count'] = 0;
                    return null;
                }

                $arg_array['count'] = $item_count;

                //排序
                if (isset($arg_array['sortname']) && !empty($arg_array['sortname']))
                    $sql .= sprintf(' ORDER BY %s %s', $arg_array['sortname'], $arg_array['sortorder']);

                //分页
                if (!isset($arg_array['page']) || !is_numeric($arg_array['page']) || $arg_array['page'] <= 0)
                    $arg_array['page'] = 1;

                $sql .= sprintf(' LIMIT %d, %d', ($arg_array['page'] - 1) * $arg_array['rp'], $arg_array['rp']);

            } else {

                //排序
                if (isset($arg_array['sortname']) && !empty($arg_array['sortname']))
                    $sql .= sprintf(' ORDER BY %s %s', $arg_array['sortname'], $arg_array['sortorder']);

            }

            //查询分类信息
            $search_sql = 'SELECT * FROM sys_branch ' . $sql;
            $temp_item_array = &$this->dbca->stmt_query($search_sql, $params_type, $params_array);
            if (!$temp_item_array)
                return null;

            foreach($temp_item_array as &$item) {
                $item_array[] = array(
                    'creator'    	=> $item->creator,
                    'createTime'   => $item->createTime,
                    'branch_id'    => $item->branch_id,
                    'branch_name'  => $item->branch_name,
                    'branch_code'  => $item->branch_code,
                    'principal'    => $item->principal,
                    'provinceName' => $item->provinceName,
                    'cityName'    	=> $item->cityName,
                    'countyName'   => $item->countyName,
                    'address'    	=> $item->address,
                    'mobile'       => $item->mobile,
                    'phone'    		=> $item->phone,
                    'logo'    		=> $item->logo ? G_IMAGE_URL . '/' . $item->logo : '',
                    'vehicle_id'  => $item->vehicle_id,
                    'useFlg'      	=> $item->useFlg
                );

            }

            return $item_array;

        } catch (Exception $e) {

            error_log($e->getMessage());
            return null;

        }
    }

    /**
     * 查询常见问题分类
     * @param $arg_array
     * @return array|null
     */
    public function searchFaqCategory(&$arg_array){

        //sql字符串
        $sql = '';

        //参数类型字符串
        $params_type = '';

        //参数值数组
        $params_array = array();

        //分类ID
        if (isset($arg_array['categoryID']) && is_numeric($arg_array['categoryID'])) {
            $sql .= ' AND categoryID = ?';
            $params_type .= 'i';
            $params_array[] = $arg_array['categoryID'];
        }

        //分类名称
        if (isset($arg_array['categoryName']) && is_numeric($arg_array['categoryName'])) {
            $sql .= ' AND categoryName like ?';
            $params_type .= 's';
            $params_array[] = '%'.$arg_array['categoryName'].'%';
        }

        if ($sql) {
            $sql = 'WHERE ' . substr($sql, 4);
        }

        try {

            if (isset($arg_array['rp']) && $arg_array['rp'] > 1) {

                //统计数量
                $search_sql = 'SELECT COUNT(*) AS item_count FROM sys_faq_category ' . $sql;
                $count_array = &$this->dbca->stmt_query($search_sql, $params_type, $params_array);
                $item_count = $count_array[0]->item_count;
                if ($item_count == 0) {
                    $arg_array['count'] = 0;
                    return null;
                }

                $arg_array['count'] = $item_count;

                //排序
                if (isset($arg_array['sortname']) && !empty($arg_array['sortname']))
                    $sql .= sprintf(' ORDER BY %s %s', $arg_array['sortname'], $arg_array['sortorder']);

                //分页
                if (!isset($arg_array['page']) || !is_numeric($arg_array['page']) || $arg_array['page'] <= 0)
                    $arg_array['page'] = 1;

                $sql .= sprintf(' LIMIT %d, %d', ($arg_array['page'] - 1) * $arg_array['rp'], $arg_array['rp']);

            } else {

                //排序
                if (isset($arg_array['sortname']) && !empty($arg_array['sortname']))
                    $sql .= sprintf(' ORDER BY %s %s', $arg_array['sortname'], $arg_array['sortorder']);

            }

            //查询分类信息
            $search_sql = 'SELECT * FROM sys_faq_category ' . $sql;
            $temp_item_array = &$this->dbca->stmt_query($search_sql, $params_type, $params_array);
            if (!$temp_item_array)
                return null;

            foreach($temp_item_array as &$item) {

                $item_array[] = array(
                    'timeStamp'     => $item->timeStamp,
                    'categoryID'    =>  $item->categoryID,
                    'categoryName'  =>  $item->categoryName,
                    'description'   =>  $item->description
                );
            }

            return $item_array;

        } catch (Exception $e) {

            error_log($e->getMessage());
            return null;

        }
    }

    /**常见问题分类的更新或添加
     * @param $arg_array
     * @return array|null
     */
    public  function addFaqCategory(&$arg_array) {

        //接收参数
        $categoryID      = $arg_array['categoryID'];
        $categoryName    = $arg_array['categoryName'];
        $description     = $arg_array['description'];
        $creator         = $arg_array['creator'];

        //实例化查询类
        $faqcategory_obj = new entity('sys_faq_category');

        //检查ID是否为空，信息是否存在
        if( !empty($categoryID) ){

            $faqcategory_obj->categoryID   = $categoryID;

            if( !$this->dbca->fetch($faqcategory_obj) ){
                return array(  'code'=>404, 'message'=>'没有找到你修改的分类信息，请刷新后重试');
            }
        }else {

            $faqcategory_obj->categoryName  = $categoryName;
            if(  $this->dbca->fetch($faqcategory_obj) ){
                return array( 'code'=>404 ,'message'=>'命名重复，请重新命名' );

            }
        }
        $faqcategory_obj->categoryName  = $categoryName;
        $faqcategory_obj->creator        = $creator ;
        $faqcategory_obj->categoryName  = $categoryName;
        $faqcategory_obj->description   = $description;


        //保存到数据库
        try{

            $this->dbca->save( $faqcategory_obj );

            if( $categoryID ){
                return array( 'code'=>200  , 'message'=>'更新成功' );

            }else{
                return array('code'=>200  ,'message'=>'保存成功' );

            }

        }catch(Exception $e){

            error_log( $e->getMessage() );
            return array('code'=>500  , 'message'=>'系统错误，请与管理员联系'  );

        }
    }

    /**
     * 删除问题分类信息
     * @param $arg_array
     * @return array|null
     */
    public function delFaqCategory (&$arg_array) {

        $delArr = $arg_array['delArr'];
        if( empty($delArr) ){

            return array('code'=>400 , 'message'=>'请选择要删除的信息' );
        }

        //统计不存在的信息的数目
        $errorNum = 0;

        //记录要删除的entity对象
        $deleteObjArr = array();

        //检查要删除的信息是否全部存在
        foreach( $delArr as $categoryID ){

            //检查信息是否存在
            $check_obj = new entity('sys_faq_category');
            $check_obj->categoryID = $categoryID;

            if( !$this->dbca->fetch( $check_obj ) )

                $errorNum++;

            $deleteObjArr[] = $check_obj;

        }

        if( $errorNum > 0 ){

            return array('message'=>'所选部分信息已不存在，请刷新页面' , 'code'=>400  );
        }

        try{

            //开启事务
            $this->dbca->beginTransaction();

            //循环删除
            foreach( $deleteObjArr as $delobj ){

                $this->dbca->delete( $delobj );

            }

            //提交事务
            $this->dbca->commit();

            return array('message'=>'删除成功', 'code'=>200);


        }catch( Exception $e ){

            //回滚事务
            $this->dbca->rollBack();

            error_log($e->getMessage());
            return array('message'=>'系统错误，请与管理员联系', 'code'=>500);
        }

    }

    /**
     *查询常见问题
     * @param $arg_array
     * @return array|null
     */
    public function searchFaqContent(&$arg_array){

        //sql字符串
        $sql = '';

        //参数类型字符串
        $params_type = '';

        //参数值数组
        $params_array = array();

        //接受参数转化
        if(isset($arg_array['s_categoryID'])&&!empty($arg_array['s_categoryID'])){
            $arg_array['categoryID'] = $arg_array['s_categoryID'];
        }

        if(isset($arg_array['s_faqTitle'])&&!empty($arg_array['s_faqTitle'])){
            $arg_array['faqTitle'] = $arg_array['s_faqTitle'];
        }

        //常见问题ID
        if (isset($arg_array['faqID']) && is_numeric($arg_array['faqID'])) {
            $sql .= ' AND faqID = ?';
            $params_type .= 'i';
            $params_array[] = $arg_array['faqID'];
        }

        //常见问题标题
        if (isset($arg_array['faqTitle']) && !empty($arg_array['faqTitle'])) {
            $sql .= ' AND faqTitle like ?';
            $params_type .= 's';
            $params_array[] = '%'.$arg_array['faqTitle'].'%';
        }

        //常见问题标题
        if (isset($arg_array['categoryID']) && !empty($arg_array['categoryID'])) {
            $sql .= ' AND categoryID = ?';
            $params_type .= 'i';
            $params_array[] = $arg_array['categoryID'];
        }

        //启用禁用
        if (isset($arg_array['useFlg']) && is_numeric($arg_array['useFlg'])) {
            $sql .= ' AND useFlg = ?';
            $params_type .= 'i';
            $params_array[] = $arg_array['useFlg'];
        }

        if ($sql) {
            $sql = 'WHERE ' . substr($sql, 4);
        }

        try {

            if (isset($arg_array['rp']) && $arg_array['rp'] > 1) {

                //统计数量
                $search_sql = 'SELECT COUNT(*) AS item_count FROM sys_faq ' . $sql;
                $count_array = &$this->dbca->stmt_query($search_sql, $params_type, $params_array);
                $item_count = $count_array[0]->item_count;
                if ($item_count == 0) {
                    $arg_array['count'] = 0;
                    return null;
                }

                $arg_array['count'] = $item_count;

                //排序
                if (isset($arg_array['sortname']) && !empty($arg_array['sortname']))
                    $sql .= sprintf(' ORDER BY %s %s', $arg_array['sortname'], $arg_array['sortorder']);

                //分页
                if (!isset($arg_array['page']) || !is_numeric($arg_array['page']) || $arg_array['page'] <= 0)
                    $arg_array['page'] = 1;

                $sql .= sprintf(' LIMIT %d, %d', ($arg_array['page'] - 1) * $arg_array['rp'], $arg_array['rp']);

            } else {

                //排序
                if (isset($arg_array['sortname']) && !empty($arg_array['sortname']))
                    $sql .= sprintf(' ORDER BY %s %s', $arg_array['sortname'], $arg_array['sortorder']);

            }

            //查询分类信息
            $search_sql = 'SELECT * FROM sys_faq ' . $sql;
            $temp_item_array = &$this->dbca->stmt_query($search_sql, $params_type, $params_array);
            if (!$temp_item_array)
                return null;

            foreach($temp_item_array as &$item) {

                $item_array[] = array(
                    'createTime'  => $item->createTime,
                    'faqID'          =>  $item->faqID,
                    'faqTitle'       =>  $item->faqTitle,
                    'content'        =>  $item->content,
                    'categoryID'     =>  $item->categoryID,
                    'categoryName'  =>  $item->categoryName,
                    'useFlg'         =>  $item->useFlg,
                    'useFlgName'    =>   $item->useFlg?'启用':'禁用'
                );

            }

            return $item_array;

        } catch (Exception $e) {

            error_log($e->getMessage());
            return null;

        }

    }

    /**
     * 常见问题启用禁用
     * @param $arg_array
     * @return array|null
     */
    public function useFaqAction(&$arg_array){

        if(!isset($arg_array['faqID']) ||!isset($arg_array['useFlg']) ){
            return array('code'=>400,'message'=>'参数错误');
        }

        try {

            $sys_faq = new entity('sys_faq');
            $sys_faq->faqID = $arg_array['faqID'];
            if($this->dbca->fetch($sys_faq)){

                $sys_faq->useFlg = $arg_array['useFlg'];

                $this->dbca->save($sys_faq);

            }else{
                return array('code'=>400,'message'=>'该条数据不存在');
            }

            if($arg_array['useFlg']){
                return array('code'=>200,'message'=>'常见问题启用成功');
            }else{
                return array('code'=>200,'message'=>'常见问题禁用成功');
            }


        }catch (Exception $e) {

            error_log($e->getMessage());
            return null;

        }

    }

    /**
     * 删除常见问题
     * @param $arg_array
     * @return array|null
     */
    public function deleteFaqContent(&$arg_array){

        if(!isset($arg_array['pid'])){
            return array('code'=>400,'message'=>'参数错误');
        }

        try{

            foreach($arg_array['pid'] as $val){
                $this->dbca->stmt_query('DELETE FROM sys_faq WHERE faqID = ?','i',$val);
            }

            return array('code'=>200,'message'=>'删除成功');

        }catch(Exception $e){
            error_log($e->getMessage());
            return null;
        }

    }

    /**
     * 保存新增常见问题
     * @param $arg_array
     * @return array|null
     */
    public function saveFaqContent(&$arg_array){

        if(!isset($arg_array['faqTitle']) || !isset($arg_array['categoryID']) || !isset($arg_array['description'])){
            return array('code'=>400,'message'=>'参数错误');
        }

        try {

            $sys_faq = new entity('sys_faq');
            $sys_faq->faqTitle = $arg_array['faqTitle'];
            $sys_faq->content = $arg_array['description'];
            $sys_faq->categoryID = $arg_array['categoryID'];
            $sys_faq->useFlg = 1;
            //获取categoryNAME

            $sys_faq_category = $this->dbca->stmt_query('select * from sys_faq_category where categoryID = ?','i',$arg_array['categoryID']);

            $categoryName = $sys_faq_category[0]->categoryName;

            $sys_faq->categoryName = $categoryName;

            $this->dbca->save($sys_faq);

            return array('code'=>200,'message'=>'保存成功');

        }catch(Exception $e){

            error_log($e->getMessage());
            return null;

        }

    }

    /**
     * 编辑常见问题
     * @param $arg_array
     * @return array|null
     */
    public function editFaqContent(&$arg_array){

        if(!isset($arg_array['faqID']) || !isset($arg_array['faqTitle']) || !isset($arg_array['categoryID']) || !isset($arg_array['description'])){
            return array('code'=>400,'message'=>'参数错误');
        }

        try {

            $sys_faq = new entity('sys_faq');
            $sys_faq->faqID = $arg_array['faqID'];
            if($this->dbca->fetch($sys_faq)){
                $sys_faq->faqTitle   = $arg_array['faqTitle'];
                $sys_faq->content    = $arg_array['description'];
                $sys_faq->categoryID = $arg_array['categoryID'];

                //获取categoryNAME

                $sys_faq_category = $this->dbca->stmt_query('select * from sys_faq_category where categoryID = ?','i',$arg_array['categoryID']);

                $categoryName = $sys_faq_category[0]->categoryName;

                $sys_faq->categoryName = $categoryName;

                $this->dbca->save($sys_faq);
            }else{
                return array('code'=>400,'message'=>'不存在该常见问题');
            }

            return array('code'=>200,'message'=>'保存成功');

        }catch(Exception $e){

            error_log($e->getMessage());
            return null;

        }

    }

}
